package com.burgeon.backend.Rest.coupon.bos;
/**
 * 创建购物券BOS
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.rest.FIndAD_Table;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CreateCouponBos extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateCouponBos(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    public String getData(Coupon coupon,List<String> goods,List<String> sotreCode ) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(coupon,goods,sotreCode), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(Coupon coupon,List<String> goods,List<String> sotreCode ) throws Exception{


        //****************************************************************
        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "C_V_VOUCHERS");
        masterobj.put("ID", "-1");

        if (null!=coupon.getVipIntegral()) {
            masterobj.put("WX_VIPINTEGRALADJ_ID__DOCNO", coupon.getVipIntegral());//积分调整单id
        }
        masterobj.put("VOUCHERS_NO","'"+coupon.getCode()+"'");//券号
        // params.put("VOU_TYPE","VOU5");//类型  VOU5优惠券
        masterobj.put("VOU_TYPE",coupon.getType());//类型  VOU5优惠券 VOU4 折扣券
        masterobj.put("VOU_DIS","0."+coupon.getDiscount());//折扣
        masterobj.put("IDENTIFY_CODE","'"+coupon.getCode()+"'");//校验码
        masterobj.put("AMT_DISCOUNT","'"+coupon.getMoney()+"'");//优惠金额
        masterobj.put("C_VIP_ID__CARDNO",coupon.getVipCode());//vip卡号
        masterobj.put("AMT_NOLES",coupon.getOriginal());//商品原价不能小于
        masterobj.put("START_DATE",coupon.getDatebeg());//起始日期
        masterobj.put("VALID_DATE",coupon.getDateend());//截止日期
        masterobj.put("IS_ONETIME","Y");//是否一次性
        masterobj.put("IS_ALLSTORE","Y");//是否应用于所有店铺
        masterobj.put("DESCRIPTION","小程序线上商城");//备注
        masterobj.put("ISACTIVE","Y");//是否可用
       // masterobj.put("ACCOUNT_LIMIT","5000");
        masterobj.put("ISSHARE_PAYTYPE","Y");
       // masterobj.put("IS_DELACCOUNT","Y");
        //masterobj.put("U_SCENARIOS","1");

        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        FIndAD_Table fIndAD_table = new FIndAD_Table(ip,account,password);
        HashMap yc_retail = fIndAD_table.getData("C_V_VOUCHERS");

        list.add(yc_retail.get("C_VOUDISLIMIT"));//4435
        list.add(yc_retail.get("C_VOUCHERS_STORE"));//4433


        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "C_VOUDISLIMIT");//限制商品

        List addList1 = new ArrayList();
        for (String goodsCode : goods) {
            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("M_PRODUCT_ID__NAME", goodsCode);

            addList1.add(map1);
        }

        if (goods.size()>0){
            ref1.put("addList", String.valueOf(addList1));
        }

        refobjs.add(ref1);

//**************************************************************
        HashMap ref2 = new HashMap();
        ref2.put("table", "C_VOUCHERS_STORE");//限制商品


        List addList2 = new ArrayList();
        for (String store : sotreCode) {
            HashMap map2 = new HashMap();//addlist内容
            map2.put("id", "-1");
            map2.put("C_STORE_ID__NAME", store);

            addList2.add(map2);
        }

        if (sotreCode.size()>0){
            ref2.put("addList", String.valueOf(addList2));
        }

        refobjs.add(ref2);
//**********************************************************************
        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        //params.put("submit", "true");//是否提交


        return DATA("ProcessOrder", params, account, password);

    }

    public static void main(String[] args) {
        CreateCouponBos g = new CreateCouponBos("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            Coupon coupon = new Coupon();
           // coupon.setVipIntegral("1231322");
            coupon.setCode("qs1142ssd111");
            coupon.setVipCode("17732141036");
            //coupon.setOriginal("200");
            coupon.setCheckno("asw22111");
            coupon.setMoney("123");
            coupon.setType("VOU4");
            coupon.setDiscount("1");
            coupon.setDatebeg("20191112");
            coupon.setDateend("20191130");
            ArrayList<String> arr = new ArrayList<String>();
            arr.add("HV940526695-N");
            ArrayList<String> store = new ArrayList<String>();

            store.add("石家庄新百一嘉");
            g.getData(coupon,arr,store);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
